﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using DigitsRecognizer.BussinesLogic;

namespace DigitsExtractor
{
    public class DigitExtractor
    {
        private int NumOfColumns = 5, NumOfRows = 7, ColumnSize, RowSize;     // rozmiary okna filtru

        public double[] LoadDigit(string fileName) 
        {
            if (fileName.Equals(string.Empty)) 
            {
                return null;
            }
            Bitmap bmp = new Bitmap(fileName, true);
            Color pixelPattern = Color.FromArgb(255, 255, 255);
            byte[,] array = new byte[bmp.Height,bmp.Width];
            //int glblIndex = 0;
            int bits = 0;

            for (int i = 0; i < bmp.Height; i++)
            {
                for (int j = 0; j < bmp.Width; j++)
                {
                    Color pixelColor = bmp.GetPixel(j, i);
                    if(pixelColor != pixelPattern)
                    {
                        array[i,j] = 1;
                        bits++;
                    }
                }
            }
            return ConvertToFilter(array);
        }

        public double[] ConvertToByte(DrawField[,] DrawArea, int columnsNum, int rowNum)
        {
            byte[,] tab = new byte[rowNum,columnsNum];
            for (int i = 0; i < rowNum; i++)
            {
                for (int j = 0; j < columnsNum; j++)
                {
                    tab[i,j] = DrawArea[i, j].IsDrawn ? Convert.ToByte(1) : Convert.ToByte(0);
                }
            }
            return ConvertToFilter(tab);
        }

        public double[] ConvertToFilter(byte[,] tab) 
        {
            double[] result = new double[NumOfRows * NumOfColumns];
            int[] tempCounter = new int[NumOfColumns];
            RowSize = tab.GetLength(0) / NumOfRows;
            ColumnSize = tab.GetLength(1) / NumOfColumns;
            int indexOfRows = 0, indexColumn = 0, indexResult = 0;

            for (int i = 0; i < tab.GetLength(0); i++)
            {
                for (int j = 0; j < tab.GetLength(1); j++)
                {
                    if (j % ColumnSize == 0 && j > 0 && indexColumn < NumOfColumns - 1) 
                    {
                        indexColumn++;
                    }
                    tempCounter[indexColumn] += tab[i, j];
                }
                indexColumn = 0;
                if ((i % RowSize == 0 && i > 0) || indexOfRows == NumOfRows) 
                {
                    for (int l = 0; l < NumOfColumns; l++, indexResult++) 
                    {
                        result[indexResult] += Convert.ToDouble(tempCounter[l]);
                        tempCounter[l] = 0;
                    }
                    if (indexOfRows < NumOfRows)
                    {
                        indexOfRows++;
                    }
                    if (indexOfRows == NumOfRows)
                    {
                        indexResult = indexResult - NumOfColumns;
                    }
                }
            }
            return result;
        }
    }
}
